//
//  HYRoleIMGView.m
//  HaiYue
//
//  Created by skyline on 2018/1/29.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYRoleIMGView.h"
@interface HYRoleIMGView ()
{
    NSArray *images;
}
@property (nonatomic, strong) HYLabel *titleLBL;
@end
@implementation HYRoleIMGView

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        images = @[@"1",@"2",@"3",@"4",@"5",@"2",@"3",@"4",@"1",@"2",@"3",@"4",@"1",@"2",@"3",@"4",@"1",@"2",@"3",@"4",@"2"];
        self.titleLBL.hidden = NO;
        [self setupSubRoleIMG];
    }
    return self;
}

- (HYLabel *)titleLBL
{
    if (!_titleLBL)
    {
        NSMutableAttributedString *strAttr = [[NSMutableAttributedString alloc] initWithString:@"角色截图 点击图片可以放大"];
        [strAttr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TitleFont],NSForegroundColorAttributeName:TEXT_BLACK_COLOR} range:NSMakeRange(0, 3)];
        [strAttr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TipsFont],NSForegroundColorAttributeName:TEXT_GRAY_COLOR} range:NSMakeRange(4, strAttr.length - 4)];
        
        _titleLBL = [[HYLabel alloc] init];
        _titleLBL.attributedText = strAttr;
        [self addSubview:_titleLBL];
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self).with.offset(GAP);
        }];
    }
    return _titleLBL;
}

- (void)setupSubRoleIMG
{
    for (int i = 0; i < images.count; i++)
    {
        NSInteger row = floor(i / 4.0);
        NSInteger column = i % 4;
        CGFloat height = (SCREEN_WIDTH - GAP * 5) / 4.0;
        
        UIButton *button = [[UIButton alloc] init];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = GAP;
        button.tag = 200 + i;
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(actionForIMGButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLBL.mas_bottom).with.offset(GAP + (GAP + height) * row);
            make.left.equalTo(self).with.offset(GAP + (GAP + height) * column);
            make.size.mas_equalTo(CGSizeMake(height, height));
        }];
    }
    
    [self updateBounds];
}

- (void)updateBounds
{
    NSInteger row = ceil(images.count / 4.0);
    CGFloat height = (SCREEN_WIDTH - GAP * 5) / 4.0;
    self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, GAP * 2  + self.titleLBL.bounds.size.height + (GAP + height) * row);
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.bounds.size.height);
    }];
}

- (void)actionForIMGButton:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(methodForHYRoleIMGViewDelegateWithImageNames:index:)])
    {
        [self.delegate methodForHYRoleIMGViewDelegateWithImageNames:images index:sender.tag - 200];
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
